package com.sdgakj.service.feedback;

import com.sdgakj.dto.base.ResCode;
import com.sdgakj.entity.Feedback;
import com.sdgakj.entity.User;
import com.sdgakj.enums.EnumIsDelete;
import com.sdgakj.repository.FeedbackDao;
import com.sdgakj.service.ServiceException;
import com.sdgakj.service.user.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springside.modules.persistence.DynamicSpecifications;
import org.springside.modules.persistence.SearchFilter;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Component
@Transactional
public class FeedbackService {
	
	@Autowired
	private FeedbackDao feedbackDao;
	
	@Autowired
	private UserService userService;
	
	public List<Feedback> getAllList(){
		return (List<Feedback>) feedbackDao.findAll();
	}
	
	public Feedback getFeedbackById(Long id) {
		return feedbackDao.findOne(id);
	}
	
	/**
	 * 保存反馈信息
	 * @param entity
	 */
	public void saveFeedback(Feedback entity) {
		feedbackDao.save(entity);
	}
	
	/**
	 * 给app端提供--添加反馈
	 * @param id
	 * @param message
	 * @param mobile
	 */
	public void createFeedback(Long id,String message,String mobile){
		Feedback feedback =new Feedback();
		feedback.setMobile(mobile);
		if("0".equals(mobile)){
			feedback.setMobile(""); 
		}
		feedback.setMessage(message);
		feedback.setDeltag(EnumIsDelete.NOTDELETE.getCode());
		this.saveFeedback(feedback);
	}

	/**
	 * 更新反馈信息
	 * @param entity
	 */
	public void updateFeedback(Feedback entity) {
		feedbackDao.save(entity);
	}
	
	
	/**
	 * 提交意见反馈的评审
	 * 
	 * @param feedbackId
	 * @param userId
	 * @param review
	 */
	public void reviewFeedback(Long feedbackId, Long userId, String review) {
		if(StringUtils.isEmpty(review)) {
			throw new ServiceException(ResCode.MGR_FB_EMPTY_REVIEW);
		}
		if(review.length() > 50) {
			throw new ServiceException(ResCode.MGR_FB_LONG_REVIEW);
		}
		
		Feedback fb = getFeedbackById(feedbackId);
		User user = userService.getUserById(userId);
		Date now = new Date();
		fb.setLastReview(review);
		fb.setLastReviewerName(user.getName());
		fb.setLastReviewTime(now);
		feedbackDao.save(fb);
		
	}
	
	public void deleteFeedback(Long id) {
		Feedback feedback = this.getFeedbackById(id);
		feedback.setDeltag(EnumIsDelete.DELETED.getCode());
		this.saveFeedback(feedback);
	}

	public Page<Feedback> getFeedbackByPage(Map<String, Object> searchParams, int pageNumber, int pageSize, String sortType) {
		PageRequest pageRequest = buildPageRequest(pageNumber, pageSize, sortType);
		Specification<Feedback> spec = buildSpecification(searchParams);
		return feedbackDao.findAll(spec, pageRequest);
	}	
	/**
	 * 创建分页请求.
	 */
	private PageRequest buildPageRequest(int pageNumber, int pagzSize,String sortType) {
		Sort sort = null;
		if ("auto".equals(sortType)) {
			sort = new Sort(Direction.DESC, "id");
		}
		return new PageRequest(pageNumber - 1, pagzSize, sort);
	}

	/**
	 * 创建动态查询条件组合.
	 */
	private Specification<Feedback> buildSpecification(Map<String, Object> searchParams) {
		Map<String, SearchFilter> filters = SearchFilter.parse(searchParams);
		Specification<Feedback> spec = DynamicSpecifications.bySearchFilter(filters.values(), Feedback.class);
		return spec;
	}
	
}
